#include <iostream>
#include <queue>
using namespace std;

const int N = 10010;

int n;
priority_queue<int, vector<int>, greater<int>> heap; //小根堆
int a, b;                                            //获取输入，工具值
int res = 0;

int main()
{
    cin >> n;
    for (int i = 0; i < n; ++i)
    {
        cin >> a;
        heap.push(a);
    }
    while (heap.size() > 1) //既还没合并完
    {
        a = heap.top();
        heap.pop();
        b = heap.top();
        heap.pop();
        res = res + a + b;
        heap.push(a + b);
    }
    cout << res;
    return 0;
}